package com.fudan.backend.dao;

import com.fudan.backend.handler.IdTypeHandler;
import com.fudan.backend.pojo.Commodity;
import com.fudan.backend.pojo.OrderItem;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderItemDao {
    @Results(id = "orderItemResultMap", value = {
            @Result(column = "order_item_id", property = "orderItemId", typeHandler = IdTypeHandler.class),
            @Result(column = "order_id", property = "orderId", typeHandler = IdTypeHandler.class),
            @Result(column = "commodity_id", property = "commodityId", typeHandler = IdTypeHandler.class),
            @Result(column = "commodity_id", property = "commodity", typeHandler = IdTypeHandler.class, javaType = Commodity.class,
            one = @One(select = "com.fudan.backend.dao.CommodityDao.selectCommodityByCommodityId"))
    })
    @Select("select * from t_order_item where order_id = #{orderId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    List<OrderItem> selectOrderItemsByOrderId(String orderId);
    @Insert("insert into t_order_item values " +
            "(#{orderItemId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{orderId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{commodityId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{commodityNum}, #{isAllowanced})")
    int insertOrderItem(OrderItem orderItem);
    @Insert("<script>" +
            "insert into t_order_item values " +
            "<foreach item='item' collection='list' separator=','>" +
            "(#{item.orderItemId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{item.orderId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{item.commodityId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{item.commodityNum}, #{item.isAllowanced})" +
            "</foreach>" +
            "</script>")
    int insertOrderItemList(List<OrderItem> list);
}
